from celery import Celery
from celery.utils.log import get_task_logger
import importlib
import inspect
import ToolBox as tb

password = 'foobared'
app_celery = Celery(
    "async_task", # app.import_name, # async_task
    broker=f'redis://:{password}@192.168.2.172:6379/0',
    backend=f'redis://:{password}@192.168.2.172:6379/1')


module_name = 'ToolBox'  # 指定要注册任务的模块名称
module_path = '/data/fengyy/project/FlaskBlueprint/ToolBox.py'  # 指定包含要注册任务的模块的绝对路径
func_names = ["qhdm_2_table","get_bilibili_video","get_bilibili_audio"] # ["tile_2_info","get_params"]
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for name, func in inspect.getmembers(module, inspect.isfunction):
    if func_names == []: 
        app_celery.task(func)
    elif name in func_names: 
        app_celery.task(func)


# celery -A Async.app_celery worker --concurrency=4 --loglevel=INFO --detach --logfile=/data/fengyy/logfile/FlaskBlueprint-Async.log

# if __name__ == '__main__':
#     log_path = r'/data/fengyy/logfile/FlaskBlueprint-Async.log'
#     args = ['worker',
#             '--concurrency=4',
#             '--loglevel=INFO',
#             '--detach', 
#             f'--logfile={log_path}']
#     app_celery.worker_main(argv=args)